﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Common;

namespace Dal
{
   public class WeiBoUser
   {
       #region Model
       private int _wbid;
       private long _tbuid;
       private string _accesstoken;
       private int? _expiresin;
       private string _refreshtoken;
       private string _openid;
       private string _openkey;
       private string _nick;
       private DateTime? _birthday;
       private int? _fansnum = 0;
       private int? _favnum;
       private string _head;
       private int? _idolnum;
       private bool _isent;
       private bool? _isrealname;
       private bool? _isvip;
       private string _location;
       private string _name;
       private int? _sex;
       private int? _tweetnum;
       private string _homepage;
       private DateTime? _regtime;
       private DateTime _addtime = DateTime.Now;
       public int WBID
       {
           set { _wbid = value; }
           get { return _wbid; }
       }
       /// <summary>
       /// 
       /// </summary>
       public long TBuid
       {
           set { _tbuid = value; }
           get { return _tbuid; }
       }
       /// <summary>
       /// 
       /// </summary>
       public string accesstoken
       {
           set { _accesstoken = value; }
           get { return _accesstoken; }
       }
       /// <summary>
       /// accesstoken过期时间秒
       /// </summary>
       public int? expiresin
       {
           set { _expiresin = value; }
           get { return _expiresin; }
       }
       /// <summary>
       /// 
       /// </summary>
       public string refreshtoken
       {
           set { _refreshtoken = value; }
           get { return _refreshtoken; }
       }
       /// <summary>
       /// 
       /// </summary>
       public string openid
       {
           set { _openid = value; }
           get { return _openid; }
       }
       /// <summary>
       /// 
       /// </summary>
       public string openkey
       {
           set { _openkey = value; }
           get { return _openkey; }
       }
       /// <summary>
       /// 
       /// </summary>
       public string nick
       {
           set { _nick = value; }
           get { return _nick; }
       }
       /// <summary>
       /// 
       /// </summary>
       public DateTime? birthday
       {
           set { _birthday = value; }
           get { return _birthday; }
       }
       /// <summary>
       /// 听众数
       /// </summary>
       public int? fansnum
       {
           set { _fansnum = value; }
           get { return _fansnum; }
       }
       /// <summary>
       /// 收藏数
       /// </summary>
       public int? favnum
       {
           set { _favnum = value; }
           get { return _favnum; }
       }
       /// <summary>
       /// 头像url
       /// </summary>
       public string head
       {
           set { _head = value; }
           get { return _head; }
       }
       /// <summary>
       /// 收听的人数
       /// </summary>
       public int? idolnum
       {
           set { _idolnum = value; }
           get { return _idolnum; }
       }
       /// <summary>
       /// 是否企业机构
       /// </summary>
       public bool isent
       {
           set { _isent = value; }
           get { return _isent; }
       }
       /// <summary>
       /// 是否实名认证
       /// </summary>
       public bool? isrealname
       {
           set { _isrealname = value; }
           get { return _isrealname; }
       }
       /// <summary>
       /// 是否认证用户
       /// </summary>
       public bool? isvip
       {
           set { _isvip = value; }
           get { return _isvip; }
       }
       /// <summary>
       /// 所在地
       /// </summary>
       public string location
       {
           set { _location = value; }
           get { return _location; }
       }
       /// <summary>
       /// 用户帐户名
       /// </summary>
       public string name
       {
           set { _name = value; }
           get { return _name; }
       }
       /// <summary>
       /// 1-男，2-女，0-未填写
       /// </summary>
       public int? sex
       {
           set { _sex = value; }
           get { return _sex; }
       }
       /// <summary>
       /// 微博数
       /// </summary>
       public int? tweetnum
       {
           set { _tweetnum = value; }
           get { return _tweetnum; }
       }
       /// <summary>
       /// 
       /// </summary>
       public string homepage
       {
           set { _homepage = value; }
           get { return _homepage; }
       }
       /// <summary>
       /// 注册时间
       /// </summary>
       public DateTime? regtime
       {
           set { _regtime = value; }
           get { return _regtime; }
       }
       /// <summary>
       /// 绑定时间
       /// </summary>
       public DateTime addtime
       {
           set { _addtime = value; }
           get { return _addtime; }
       }
       #endregion Model

       #region  Method
       /// <summary>
       /// 增加一条数据
       /// </summary>
       public int Add()
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append("insert into [WeiBoUser] (");
           strSql.Append("TBuid,accesstoken,expiresin,refreshtoken,openid,openkey,nick,birthday,fansnum,favnum,head,idolnum,isent,isrealname,isvip,location,name,sex,tweetnum,homepage,regtime)");
           strSql.Append(" values (");
           strSql.Append("@TBuid,@accesstoken,@expiresin,@refreshtoken,@openid,@openkey,@nick,@birthday,@fansnum,@favnum,@head,@idolnum,@isent,@isrealname,@isvip,@location,@name,@sex,@tweetnum,@homepage,@regtime)");
           strSql.Append(";select @@IDENTITY");
           SqlParameter[] parameters = {
					new SqlParameter("@TBuid", SqlDbType.BigInt,8),
					new SqlParameter("@accesstoken", SqlDbType.VarChar,50),
					new SqlParameter("@expiresin", SqlDbType.Int,4),
					new SqlParameter("@refreshtoken", SqlDbType.VarChar,50),
					new SqlParameter("@openid", SqlDbType.VarChar,50),
					new SqlParameter("@openkey", SqlDbType.VarChar,50),
					new SqlParameter("@nick", SqlDbType.NVarChar,50),
					new SqlParameter("@birthday", SqlDbType.SmallDateTime),
					new SqlParameter("@fansnum", SqlDbType.Int,4),
					new SqlParameter("@favnum", SqlDbType.Int,4),
					new SqlParameter("@head", SqlDbType.VarChar,50),
					new SqlParameter("@idolnum", SqlDbType.Int,4),
					new SqlParameter("@isent", SqlDbType.Bit,1),
					new SqlParameter("@isrealname", SqlDbType.Bit,1),
					new SqlParameter("@isvip", SqlDbType.Bit,1),
					new SqlParameter("@location", SqlDbType.NVarChar,50),
					new SqlParameter("@name", SqlDbType.NVarChar,50),
					new SqlParameter("@sex", SqlDbType.TinyInt,1),
					new SqlParameter("@tweetnum", SqlDbType.Int,4),
					new SqlParameter("@homepage", SqlDbType.VarChar,50),
					new SqlParameter("@regtime", SqlDbType.SmallDateTime)};
           parameters[0].Value = TBuid;
           parameters[1].Value = accesstoken;
           parameters[2].Value = expiresin;
           parameters[3].Value = refreshtoken;
           parameters[4].Value = openid;
           parameters[5].Value = openkey;
           parameters[6].Value = nick;
           parameters[7].Value = birthday;
           parameters[8].Value = fansnum;
           parameters[9].Value = favnum;
           parameters[10].Value = head;
           parameters[11].Value = idolnum;
           parameters[12].Value = isent;
           parameters[13].Value = isrealname;
           parameters[14].Value = isvip;
           parameters[15].Value = location;
           parameters[16].Value = name;
           parameters[17].Value = sex;
           parameters[18].Value = tweetnum;
           parameters[19].Value = homepage;
           parameters[20].Value = regtime;
           int rows = 0;
           try
           {
               rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
           }
           catch (Exception e)
           {
               if (e.Message.Contains("插入重复键"))
               {
                   rows = Update();
               }
           }
           return rows;
       }
       /// <summary>
       /// 更新一条数据
       /// </summary>
       public int Update()
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append("update [WeiBoUser] set ");
           strSql.Append("accesstoken=@accesstoken,");
           strSql.Append("expiresin=@expiresin,");
           strSql.Append("refreshtoken=@refreshtoken,");
           strSql.Append("openkey=@openkey,");
           strSql.Append("nick=@nick,");
           strSql.Append("birthday=@birthday,");
           strSql.Append("fansnum=@fansnum,");
           strSql.Append("favnum=@favnum,");
           strSql.Append("head=@head,");
           strSql.Append("idolnum=@idolnum,");
           strSql.Append("isent=@isent,");
           strSql.Append("isrealname=@isrealname,");
           strSql.Append("isvip=@isvip,");
           strSql.Append("location=@location,");
           strSql.Append("name=@name,");
           strSql.Append("sex=@sex,");
           strSql.Append("tweetnum=@tweetnum,");
           strSql.Append("homepage=@homepage,");
           strSql.Append("regtime=@regtime");
           strSql.Append(" where openid=@openid ");
           SqlParameter[] parameters = {
					new SqlParameter("@accesstoken", SqlDbType.VarChar,50),
					new SqlParameter("@expiresin", SqlDbType.Int,4),
					new SqlParameter("@refreshtoken", SqlDbType.VarChar,50),
					new SqlParameter("@openkey", SqlDbType.VarChar,50),
					new SqlParameter("@nick", SqlDbType.NVarChar,50),
					new SqlParameter("@birthday", SqlDbType.SmallDateTime),
					new SqlParameter("@fansnum", SqlDbType.Int,4),
					new SqlParameter("@favnum", SqlDbType.Int,4),
					new SqlParameter("@head", SqlDbType.VarChar,50),
					new SqlParameter("@idolnum", SqlDbType.Int,4),
					new SqlParameter("@isent", SqlDbType.Bit,1),
					new SqlParameter("@isrealname", SqlDbType.Bit,1),
					new SqlParameter("@isvip", SqlDbType.Bit,1),
					new SqlParameter("@location", SqlDbType.NVarChar,50),
					new SqlParameter("@name", SqlDbType.NVarChar,50),
					new SqlParameter("@sex", SqlDbType.TinyInt,1),
					new SqlParameter("@tweetnum", SqlDbType.Int,4),
					new SqlParameter("@homepage", SqlDbType.VarChar,50),
					new SqlParameter("@regtime", SqlDbType.SmallDateTime),
					new SqlParameter("@openid", SqlDbType.VarChar)};
           parameters[0].Value = accesstoken;
           parameters[1].Value = expiresin;
           parameters[2].Value = refreshtoken;
           parameters[3].Value = openkey;
           parameters[4].Value = nick;
           parameters[5].Value = birthday;
           parameters[6].Value = fansnum;
           parameters[7].Value = favnum;
           parameters[8].Value = head;
           parameters[9].Value = idolnum;
           parameters[10].Value = isent;
           parameters[11].Value = isrealname;
           parameters[12].Value = isvip;
           parameters[13].Value = location;
           parameters[14].Value = name;
           parameters[15].Value = sex;
           parameters[16].Value = tweetnum;
           parameters[17].Value = homepage;
           parameters[18].Value = regtime;
           parameters[19].Value = openid;

           return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
       }
       /// <summary>
       /// 删除一条数据
       /// </summary>
       public bool Delete(long TBuid, string openid)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append("delete from [WeiBoUser] ");
           strSql.Append(" where TBuid=@TBuid and openid=@openid ");
           SqlParameter[] parameters = {
					new SqlParameter("@TBuid", SqlDbType.BigInt),
					new SqlParameter("@openid", SqlDbType.VarChar)};
           parameters[0].Value = TBuid;
           parameters[1].Value = openid;

           int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
           return rows > 0;
       }

       /// <summary>
       /// 是否存在该记录
       /// </summary>
       public bool Exists(long TBuid, string openid)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append("select count(1) from [WeiBoUser]");
           strSql.Append(" where TBuid=@TBuid and openid=@openid ");

           SqlParameter[] parameters = {
					new SqlParameter("@TBuid", SqlDbType.BigInt),
					new SqlParameter("@openid", SqlDbType.VarChar)};
           parameters[0].Value = TBuid;
           parameters[1].Value = openid;

           return DbHelperSQL.Exists(strSql.ToString(), parameters);
       }
       /// <summary>
       /// 获得某用户绑定的微博账户数
       /// </summary>
       public int GetCount(string TBuid)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append("select count(*) ");
           strSql.Append(" FROM [WeiBoUser] Where  TBuid="+TBuid);
           return (int)DbHelperSQL.GetSingle(strSql.ToString());
       }
       /// <summary>
       /// 获得数据列表
       /// </summary>
       public DataTable GetList(string strWhere)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append("select * ");
           strSql.Append(" FROM [WeiBoUser] ");
           if (strWhere.Trim() != "")
           {
               strSql.Append(" where " + strWhere);
           }
           return DbHelperSQL.GetDataTable(strSql.ToString());
       }
       #endregion  Method
    }
}
